package com.stats.ultimate;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Stats extends Activity {
	// Declare variables to hold SQLiteDatabase and database helper variables
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare variable to hold team id
	int id;
	
	// Declare variable to hold number of players on this team
	int num_players;
	
	// Declare 4 variables to hold teams stats
	int team_goals;
	int team_assists;
	int team_d;
	int team_to;
	
	// Declare 4 TextViews to display team's stats
	TextView team_goals_view;
	TextView team_assists_view;
	TextView team_d_view;
	TextView team_to_view;
	
	// Declare arrays of ints to hold stats for each player
	ArrayList<Integer> goals = new ArrayList<Integer>();
	ArrayList<Integer> assists = new ArrayList<Integer>();
	ArrayList<Integer> d = new ArrayList<Integer>();
	ArrayList<Integer> to = new ArrayList<Integer>();
	
	// Declare array of TextViews to hold displays for each players name
	ArrayList<TextView> names = new ArrayList<TextView>();
	
	// Declare arrays of TextViews to hold displays of stats for each player
	ArrayList<TextView> goals_views = new ArrayList<TextView>();
	ArrayList<TextView> assists_views = new ArrayList<TextView>();
	ArrayList<TextView> d_views = new ArrayList<TextView>();
	ArrayList<TextView> to_views = new ArrayList<TextView>();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stats);
        
        // Instantiate new db_helper 
        db_helper = new DatabaseHelper(this);
    	
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so get id from savedInstanceState bundle
        	id = savedInstanceState.getInt("id");
        }
        
        // Else this is a new activity
        else{
        	// If so get id from previous activity
        	id = this.getIntent().getIntExtra("id", 1);
        }

        // Instantiate title textView
        TextView title = (TextView) findViewById(R.id.title);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Read this teams name from the database
        String team_name = db_helper.getTeamName(db, id);

        // Get number of players on this team from the database
        num_players = db_helper.getNumberPlayers(db, id);
        
        // Initialize team stats to 0
        team_goals = 0;
        team_assists = 0;
        team_d = 0;
        team_to = 0;
        
        // Iterate through every player on this team
        for(int i = 1; i <= num_players; i++){
        	// Get this players stats
        	int[] stats = db_helper.getPlayerStats(db, id, i);
        	
        	// Add this players stats to corresponding stats arrays
        	goals.add(stats[0]);
        	assists.add(stats[1]);
        	d.add(stats[2]);
        	to.add(stats[3]);
        	
        	// Add this players stats to cumulative team stats
        	team_goals += stats[0];
        	team_assists += stats[1];
        	team_d += stats[2];
        	team_to += stats[3];
        }
        
        // Close the database
        db.close();
        
        // Set the title of this page to be the team name
        title.setText(team_name);
        
        // Initialize text views to hold team stats
        team_goals_view = (TextView) findViewById(R.id.teamGoals);
        team_assists_view = (TextView) findViewById(R.id.teamAssists);
        team_d_view = (TextView) findViewById(R.id.teamD);
        team_to_view = (TextView) findViewById(R.id.teamTo);
        
        // Format strings that hold team stat values and display them in text views
        team_goals_view.setText(String.format("%d", team_goals));
        team_assists_view.setText(String.format("%d", team_assists));
        team_d_view.setText(String.format("%d", team_d));
        team_to_view.setText(String.format("%d", team_to));
        
        // Initialize relative layout that is inside a scrollview defined in layout
        RelativeLayout l1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
        
        // Declare textview to hold first players name
        TextView first_player = new TextView(this);
        
        // Open the database for reading
        db = db_helper.getWritableDatabase();
        
        // Get first players name and make it the text of first_player
        first_player.setText(db_helper.getPlayerName(db, id, 1));
        
        // Close the database
        db.close();

		// Define the layout parameters to be used for first_player
		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

		// Set the margins of first player to be 10 on the left, and 20 on the top and bottom
		params1.setMargins(10, 20, 0, 20);
		
		// Set the parameters of first_player
		first_player.setLayoutParams(params1);
		
		// Set the id of first_player to be 101
		first_player.setId(101);
		
		// Add first_player to layout
		l1.addView(first_player);
		
		// Add first_player to names array
		names.add(first_player);
				
		// Declare textview to hold first players to view
		TextView first_to_view = new TextView(this);
		
		// Get first players to's, format it, and make it the text of first_to_view
        first_to_view.setText(String.format("%d", to.get(0)));

		// Define the layout parameters to be used for first_to_view
		RelativeLayout.LayoutParams params5 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

		// Set first_to_view to be right aligned in parent
		params5.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		
		// Set right margin of first_to_view to be 30
		params5.rightMargin = 30;
		
		// Set first_to_view to be top aligned with first_player
		params5.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_to_view
		first_to_view.setLayoutParams(params5);
		
		// Set the id of first_to_view to be 501
		first_to_view.setId(501);
		
		// Add first_to_view to l1
		l1.addView(first_to_view);
		
		// Add first_to_view to to_views array
		to_views.add(first_to_view);
		
		// Declare textview to hold first players d view
		TextView first_d_view = new TextView(this);
		
		// Get first players d's, format it, and make it the text of first_d_view
        first_d_view.setText(String.format("%d", d.get(0)));

		// Define the layout parameters to be used for first_d_view
		RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

		// Set first_d_view to be to the left of first_to_view
		params4.addRule(RelativeLayout.LEFT_OF, 501);
		
		// Set right margin of first_d_view to be 27
		params4.rightMargin = 27;
		
		// Set first_d_view to be top aligned with first_player
		params4.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_d_view
		first_d_view.setLayoutParams(params4);
		
		// Set the id of first_d_view to be 401
		first_d_view.setId(401);
		
		// Add first_d_view to l1
		l1.addView(first_d_view);
		
		// Add first_d_view to d_views array
		d_views.add(first_d_view);

		// Declare textview to hold first players assists view
		TextView first_assists_view = new TextView(this);
		
		// Get first players assists, format it, and make it the text of first_assists_view
        first_assists_view.setText(String.format("%d", assists.get(0)));

		// Define the layout parameters to be used for first_assists_view
		RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

		// Set first_assists_view to be to the left of first_d_view
		params3.addRule(RelativeLayout.LEFT_OF, 401);
		
		// Set right margin of first_assists_view to be 55
		params3.rightMargin = 55;
		
		// Set first_assists_view to be top aligned with first_player
		params3.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_assists_view
		first_assists_view.setLayoutParams(params3);
		
		// Set the id of first_assists_view to be 301
		first_assists_view.setId(301);
		
		// Add first_assists_view to l1
		l1.addView(first_assists_view);
		
		// Add first_assists_view to assists_views array
		assists_views.add(first_assists_view);

		// Declare textview to hold first players goals view
		TextView first_goals_view = new TextView(this);
		
		// Get first players goals, format it, and make it the text of first_goals_view
        first_goals_view.setText(String.format("%d", goals.get(0)));

		// Define the layout parameters to be used for first_goals_view
		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
        
		// Set first_goals_view to be to the left of first_assists_view
		params2.addRule(RelativeLayout.LEFT_OF, 301);
		
		// Set right margin of first_goals_view to be 48
		params2.rightMargin = 48;
		
		// Set first_goals_view to be top aligned with first_player
		params2.addRule(RelativeLayout.ALIGN_TOP, 101);
		
		// Set the parameters of first_goals_view
		first_goals_view.setLayoutParams(params2);
		
		// Set the id of first_goals_view to be 201
		first_goals_view.setId(201);
		
		// Add first_goals_view to l1
		l1.addView(first_goals_view);
		
		// Add first_goals_view to goals_views array
		goals_views.add(first_goals_view);
		
		// Iterate through remaining players
		for(int i = 2; i <= num_players; i++){
			// Declare textview to hold this players name
	        TextView temp_player = new TextView(this);
	        
	        // Open the database for reading
	        db = db_helper.getWritableDatabase();
	        
	        // Get this players name and make it the text of first_player
	        temp_player.setText(db_helper.getPlayerName(db, id, i));
	        
	        // Close the database
	        db.close();

			// Define the layout parameters to be used for temp_player
			RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

			// Set temp_player to be below the last player
			temp_params1.addRule(RelativeLayout.BELOW, 100+i-1);

			// Set temp_player to be left aligned with the last player
			temp_params1.addRule(RelativeLayout.ALIGN_LEFT, 100+i-1);
			
			// Set the margins of temp_player to be 20 on the top and bottom
			temp_params1.setMargins(0, 20, 0, 20);
			
			// Set the parameters of temp_player
			temp_player.setLayoutParams(temp_params1);
			
			// Set the id of temp_player to be 100+i
			temp_player.setId(100+i);
			
			// Add temp_player to layout
			l1.addView(temp_player);
			
			// Add temp_player to names array
			names.add(temp_player);
					
			// Declare textview to hold this players to view
			TextView temp_to_view = new TextView(this);
			
			// Get this players to's, format it, and make it the text of temp_to_view
	        temp_to_view.setText(String.format("%d", to.get(i-1)));

			// Define the layout parameters to be used for first_to_view
			RelativeLayout.LayoutParams temp_params5 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

			// Set temp_to_view to be right aligned with the view above it
			temp_params5.addRule(RelativeLayout.ALIGN_RIGHT, 500+i-1);
			
			// Set temp_to_view to be top aligned with temp_player
			temp_params5.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_to_view
			temp_to_view.setLayoutParams(temp_params5);
			
			// Set the id of temp_to_view to be 500+i
			temp_to_view.setId(500+i);
			
			// Add temp_to_view to l1
			l1.addView(temp_to_view);
			
			// Add temp_to_view to to_views array
			to_views.add(temp_to_view);
			
			// Declare textview to hold this players d view
			TextView temp_d_view = new TextView(this);
			
			// Get this players d's, format it, and make it the text of temp_d_view
	        temp_d_view.setText(String.format("%d", d.get(i-1)));

			// Define the layout parameters to be used for temp_d_view
			RelativeLayout.LayoutParams temp_params4 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

			// Set temp_d_view to be right aligned with the view above it
			temp_params4.addRule(RelativeLayout.ALIGN_RIGHT, 400+i-1);
			
			// Set temp_d_view to be top aligned with temp_player
			temp_params4.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_d_view
			temp_d_view.setLayoutParams(temp_params4);
			
			// Set the id of temp_d_view to be 400+i
			temp_d_view.setId(400+i);
			
			// Add temp_d_view to l1
			l1.addView(temp_d_view);
			
			// Add temp_d_view to d_views array
			d_views.add(temp_d_view);

			// Declare textview to hold this players assists view
			TextView temp_assists_view = new TextView(this);
			
			// Get this players assists, format it, and make it the text of temp_assists_view
	        temp_assists_view.setText(String.format("%d", assists.get(i-1)));

			// Define the layout parameters to be used for temp_assists_view
			RelativeLayout.LayoutParams temp_params3 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		

			// Set temp_assists_view to be right aligned with the view above it
			temp_params3.addRule(RelativeLayout.ALIGN_RIGHT, 300+i-1);
			
			// Set temp_assists_view to be top aligned with temp_player
			temp_params3.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_assists_view
			temp_assists_view.setLayoutParams(temp_params3);
			
			// Set the id of temp_assists_view to be 300+i
			temp_assists_view.setId(300+i);
			
			// Add temp_assists_view to l1
			l1.addView(temp_assists_view);
			
			// Add temp_assists_view to assists_views array
			assists_views.add(temp_assists_view);

			// Declare textview to hold this players goals view
			TextView temp_goals_view = new TextView(this);
			
			// Get this players goals, format it, and make it the text of temp_goals_view
	        temp_goals_view.setText(String.format("%d", goals.get(i-1)));

			// Define the layout parameters to be used for temp_goals_view
			RelativeLayout.LayoutParams temp_params2 = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);		
	        
			// Set temp_goals_view to be right aligned with the view above it
			temp_params2.addRule(RelativeLayout.ALIGN_RIGHT, 200+i-1);
			
			// Set temp_goals_view to be top aligned with temp_player
			temp_params2.addRule(RelativeLayout.ALIGN_TOP, temp_player.getId());
			
			// Set the parameters of temp_goals_view
			temp_goals_view.setLayoutParams(temp_params2);
			
			// Set the id of temp_goals_view to be 200+i
			temp_goals_view.setId(200+i);
			
			// Add temp_goals_view to l1
			l1.addView(temp_goals_view);
			
			// Add temp_goals_view to goals_views array
			goals_views.add(temp_goals_view);
		}
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put id into outState bundle
    	outState.putInt("id", id);
    }
}
